package com.beehoo.dcportal.admin.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.beehoo.dcportal.admin.dao.CommunityDao;
import com.beehoo.dcportal.common.base.BaseDao;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.CommunityBean;

public class CommunityDaoImpl extends BaseDao implements CommunityDao {

	private static final Logger logger = Logger.getLogger(CommunityDaoImpl.class);
	
	@Override
	public String addCommunity(CommunityBean communityBean)
			throws BaseException {
		String id = null;
		try {
			id = (String) sqlMapClient.insert("admin-community.addCommunity", communityBean);
			logger.info("中心管理-添加社区|#[中心编号：" 
					+ communityBean.getStreetId()
					+ "],[社区名称：" + communityBean.getCommunityName()
					+ "],[社区头像：" + communityBean.getCommunityPhoto()
					+ "],[社区负责人：" + communityBean.getCommunityOwner()
					+ "],[社区职责：" + communityBean.getCommunityDuty()
					+ "],[社区电话：" + communityBean.getCommunityTel()
					+ "],[社区Email：" + communityBean.getCommunityEmail()
					+ "],[社区详细：" + communityBean.getCommunityInfo()
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("中心管理-添加社区|#[中心编号：" 
					+ communityBean.getStreetId()
					+ "],[社区名称：" + communityBean.getCommunityName()
					+ "],[社区头像：" + communityBean.getCommunityPhoto()
					+ "],[社区负责人：" + communityBean.getCommunityOwner()
					+ "],[社区职责：" + communityBean.getCommunityDuty()
					+ "],[社区电话：" + communityBean.getCommunityTel()
					+ "],[社区Email：" + communityBean.getCommunityEmail()
					+ "],[社区详细：" + communityBean.getCommunityInfo()
					+ "],失败!异常："+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.leader.create.error");
		}
		return id;
	}

	@Override
	public int delCommunity(String communityId) throws BaseException {
		int ret = 0;
		try {
			ret = sqlMapClient.delete("admin-community.delCommunity", communityId);
			logger.info("中心管理-删除社区|#[社区编号：" + communityId + "],成功!");
		} catch (SQLException e) {
			logger.error("中心管理-删除社区|#[社区编号：" + communityId
					+ "],失败!异常："+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.community.del.error");
		}
		return ret;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CommunityBean> queryCommunities(Map<String, Object> paramMap)
			throws BaseException {
		List<CommunityBean> beanList = null;
		try {
			beanList = sqlMapClient.queryForList("admin-community.queryCommunity", paramMap);
			logger.info("中心管理-查询社区|#[中心编号：" 
					+ paramMap.get("streetId")
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("中心管理-查询社区|#[中心编号：" 
					+ paramMap.get("streetId")
					+ "],失败!异常："+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.community.query.error");
		}
		return beanList;
	}

	@Override
	public CommunityBean queryCommunity(Map<String, Object> paramMap)
			throws BaseException {
		CommunityBean bean = null;
		try {
			bean = (CommunityBean) sqlMapClient.queryForObject("admin-community.queryCommunity", paramMap);
			logger.info("中心管理-查询社区|#[中心编号：" 
					+ paramMap.get("streetId")
					+ "],[社区编号：" + paramMap.get("communityId")
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("中心管理-查询社区|#[中心编号：" 
					+ paramMap.get("streetId")
					+ "],[社区编号：" + paramMap.get("communityId")
					+ "],失败!异常："+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.community.query.error");
		}
		return bean;
	}

	@Override
	public int updateCommunity(CommunityBean communityBean)
			throws BaseException {
		int ret = 0;
		try {
			ret = sqlMapClient.update("admin-community.updateCommunity", communityBean);
			logger.info("中心管理-更新社区|#[中心编号：" 
					+ communityBean.getStreetId()
					+ "],[社区编号：" + communityBean.getCommunityId()
					+ "],[社区名称：" + communityBean.getCommunityName()
					+ "],[社区头像：" + communityBean.getCommunityPhoto()
					+ "],[社区负责人：" + communityBean.getCommunityOwner()
					+ "],[社区职责：" + communityBean.getCommunityDuty()
					+ "],[社区电话：" + communityBean.getCommunityTel()
					+ "],[社区Email：" + communityBean.getCommunityEmail()
					+ "],[社区详细：" + communityBean.getCommunityInfo()
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("中心管理-更新社区|#[中心编号：" 
					+ communityBean.getStreetId()
					+ "],[社区编号：" + communityBean.getCommunityId()
					+ "],[社区名称：" + communityBean.getCommunityName()
					+ "],[社区头像：" + communityBean.getCommunityPhoto()
					+ "],[社区负责人：" + communityBean.getCommunityOwner()
					+ "],[社区职责：" + communityBean.getCommunityDuty()
					+ "],[社区电话：" + communityBean.getCommunityTel()
					+ "],[社区Email：" + communityBean.getCommunityEmail()
					+ "],[社区详细：" + communityBean.getCommunityInfo()
					+ "],失败!异常："+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.community.update.error");
		}
		return ret;
	}

}
